﻿using System;
using System.Linq;

namespace IniFileManager
{
	/// <summary>
	/// Class of elements containing "boolean" value.
	/// </summary>
	sealed public class IniBoolElement : IniValueElement
	{
		private string valueInFile;
		private bool value;
		/// <summary>
		/// Constructor of IniUnsignedElement.
		/// </summary>
		/// <param name="value">Value which is stored in element.</param>
		/// <param name="valueInFile">Value which is showed.</param>
		public IniBoolElement( bool value, string valueInFile )
		{
			this.valueInFile = valueInFile;
			this.value = value;
		}

		/// <summary>
		/// Constructor of IniUnsignedElement.
		/// </summary>
		/// <param name="value">Value which is stored in element.</param>
		public IniBoolElement( bool value )
		{
			this.value = value;
			this.valueInFile = value.ToString();
		}
		internal static IniElement Parse( string value )
		{
			if( TextConstants.boolTrueSet.Contains( value ) )
			{
				return new IniBoolElement( true, value );
			}

			if( TextConstants.boolFalseSet.Contains( value ) )
			{
				return new IniBoolElement( false, value );
			}

			throw new FormatException( "This element should be boolean." );
		}

		/// <summary>
		/// Method which is used to writing self to ini file. 
		/// </summary>
		/// <returns>String which will be written to ini file.</returns>
		public override string WriteSelf()
		{
			return this.valueInFile;
		}

		/// <summary>
		/// Gets or sets value stored in element.
		/// </summary>
		public override object Value
		{
			get
			{
				return this.value;
			}
			set
			{
				this.value = (bool)value;
			}
		}
	}
}
